package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.ParamsUtil;
import com.ksy.oss.OssClient;
import com.ksy.oss.OssResult;
import com.ksy.video.RecordVideoActivity;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.ImageApi;
import com.tech.connect.api.QuznZiHttp;
import com.tech.connect.view.ImageAdder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PublishQuanZiActivity extends BaseActivity {
    private ImageAdder imageAdder;
    private EditText et;
    private ImageView ivVideo;
    private boolean isVideo;
    private String videoFilePath;
    private String videoThumbPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("服务圈").setRightText("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = et.getText().toString().trim();
                final Map<String, Object> map = QuznZiHttp.getMap();
                map.put("areaCode", "0");
                map.put("cityCode", "0");
                map.put("provinceCode", "0");
                map.put("content", content);
                if (isVideo) {
                    if (isEmpty(videoFilePath)) {
                        showToast("请拍摄视频");
                        return;
                    }
                    map.put("containsImage", 0);
                    map.put("containsVideo", 1);
                    showDialog();
                    List<ParamsUtil.ParamsTwo<OssClient.FileType, String>> twoList = new ArrayList<>();
                    twoList.add(ParamsUtil.getInstance().create(OssClient.FileType.Image, videoThumbPath));
                    twoList.add(ParamsUtil.getInstance().create(OssClient.FileType.Video, videoFilePath));
                    OssClient.getInstance().update(activity, twoList, new OssClient.UpdatesListener() {
                        @Override
                        public void onFinish(final boolean isSuccess, final List<OssResult> results, List<String> ossUrls) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (isSuccess) {
                                        String videoCover = results.get(0).aliUrl;
                                        String videos = results.get(1).aliUrl;
                                        map.put("videoCover", videoCover);
                                        map.put("videos", videos);
                                        update(map);
                                    } else {
                                        hideDialog();
                                        showToast("视频上传失败");
                                    }
                                }
                            });

                        }
                    });
                } else {
                    List<String> images = imageAdder.getImages();
                    if (isEmpty(content) && images.isEmpty()) {
                        showToast("请输入内容或者选择图片");
                        return;
                    }
                    if (!images.isEmpty()) {
                        map.put("containsImage", 1);
                        map.put("containsVideo", 0);

                        showDialog();
                        List<ParamsUtil.ParamsTwo<OssClient.FileType, String>> twoList = new ArrayList<>();
                        for (String image : images) {
                            twoList.add(ParamsUtil.getInstance().create(OssClient.FileType.Image, image));
                        }
                        OssClient.getInstance().update(activity, twoList, new OssClient.UpdatesListener() {
                            @Override
                            public void onFinish(final boolean isSuccess, List<OssResult> results, final List<String> ossUrls) {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (isSuccess) {
                                            map.put("images", ImageApi.getImageParams(ossUrls));
                                            update(map);
                                        } else {
                                            hideDialog();
                                            showToast("图片上传失败");
                                        }
                                    }
                                });
                            }
                        });
                    } else {
                        map.put("containsImage", 0);
                        map.put("containsVideo", 0);
                        update(map);
                    }
                }
            }
        });
        setContentView(R.layout.activity_publish_quanzi);
        Intent intent = getIntent();
        isVideo = intent.getBooleanExtra("isVideo", false);
        initView();
        if (isVideo) {
            imageAdder.setVisibility(View.GONE);
            ivVideo.setVisibility(View.VISIBLE);
        } else {
            imageAdder.setVisibility(View.VISIBLE);
            ivVideo.setVisibility(View.GONE);
        }
        loadData();
    }

    private void update(Map<String, Object> map) {
        QuznZiHttp.create(map, new BaseEntityOb<Object>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, Object data, String msg) {
                hideDialog();
                if (isSuccess) {
                    setResult(RESULT_OK);
                    destroyActivity();
                }

            }
        });
    }

    private void initView() {
        et = findViewById(R.id.et);
        imageAdder = findViewById(R.id.imageAdder);
        ivVideo = findViewById(R.id.ivCover);
        imageAdder.bindContext(activity, 6);
        ivVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecordVideoActivity.start(activity, 996);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            imageAdder.onImagePickResult(requestCode, resultCode, data);
        }
        if (requestCode == 996 && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                videoFilePath = data.getStringExtra("videoFilePath");
                videoThumbPath = data.getStringExtra("videoThumbPath");
                ImageLoader.getInstance().load(activity, videoThumbPath, ivVideo);
            } else {
                showToast("视频不可用");
            }
        }
    }

    private void loadData() {

    }

}